home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cug173.zip / HWORD.LXI < prev    next >
Text File  |  1980-01-01  |  3KB  |  110 lines

  1.  
  2. /*
  3.  * Word recognizer (with hyphenation)
  4.  *
  5.  * This program acts as a very simple filter for files of
  6.  * text that may have hyphenated words at the end of an input
  7.  * line.  Output consists of each word on a seperate line
  8.  * with hyphenated words rejoined.  Note: a word is said to
  9.  * start with the first alphabetic character and end with the
  10.  * last alphabetic character.  Embedded graphics will be removed.
  11.  */
  12.  
  13. /*
  14.  * Basic elements
  15.  */
  16. white   = [\n\r\t ];            /* End of a word        */
  17. bol     = [\n] white*;          /* Beginning of a line  */
  18. eol     = [\0\n\r];             /* End of input line    */
  19. letter  = [A-Za-z];             /* Is  a letter         */
  20. graphic = [!-@\[-`{-~];         /* Not a letter         */
  21. text    = [!-~];                /* All printing chars.  */
  22. garbage = [\1-\377];            /* Whatever remains     */
  23.  
  24. /*
  25.  * A word contains "junk", at least one letter, then at
  26.  * least another letter, then more junk.
  27.  *
  28.  * A hyphenated word is a word-<NEWLINE> followed by a word
  29.  * on the next line.
  30.  */
  31.  
  32. word    = graphic* letter text* letter graphic*;
  33. junk    = (letter white) | (graphic* white);
  34.  
  35. %{
  36.  
  37. main()
  38. {
  39.         while (yylex())
  40.                 ;
  41. }
  42. %}
  43.  
  44. %%
  45.  
  46. /*
  47.  * A hyphenated word
  48.  */
  49.  
  50. word "-" eol / bol letter letter
  51.         {
  52.                 output(TRUE);
  53.                 return(LEXSKIP);
  54.         }
  55. /*
  56.  * An ordinary word
  57.  */
  58.  
  59. word    {
  60.                 output(FALSE);
  61.                 return(LEXSKIP);
  62.         }
  63.  
  64.  
  65. /*
  66.  * Junk (one letter words or all graphics)
  67.  */
  68.  
  69. junk
  70.         {
  71.                 return(LEXSKIP);
  72.         }
  73.  
  74. /*
  75.  * Other stuff
  76.  */
  77.  
  78. eol | white | garbage
  79.         {
  80.                 return(LEXSKIP);
  81.         }
  82.  
  83. %%
  84.  
  85. output(flag)
  86. int             flag;
  87. /*
  88.  * Output the current token.  The parameter is TRUE if this is
  89.  * the start of a hyphenated word.
  90.  */
  91. {
  92.         register char   *tokptr;        /* Locate token start   */
  93.         char            *tokend;        /* Locate token end     */
  94.         char    *token();
  95.         char buffer[100]; 
  96.  
  97.         tokptr = token(&tokend);
  98.         /*
  99.          * Skip over leading and trailing non-alpha stuff
  100.          */
  101.         while (!isalpha(*tokptr) && tokptr < tokend)
  102.                 tokptr++;
  103.         while (!isalpha(*--tokend) && tokend > tokptr);
  104.         strncpy(buffer, tokptr, tokend-tokptr+1);
  105.         buffer[tokend-tokptr+1]='\0';
  106.         printf("%s", buffer);
  107.         if (!flag)
  108.                 putchar('\n');
  109. }
  110.